using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace FanDongping.Stock
{
    public class TradeConfig
    {
        private decimal sellTradeRatio = 0.0016M, buyTradeRatio = 0.0016M, stampTaxRatio = 0.001M, otherFee = 1.0M;
        private bool modified = true;
        private static string tradeCfgPath = AppDomain.CurrentDomain.BaseDirectory + "TradeConfig.xml";

        [XmlIgnore]
        public bool Modified
        {
            get { return modified; }
            set { modified = value; }
        }

        public decimal OtherFee
        {
            get { return otherFee; }
            set
            {
                if (otherFee != value)
                {
                    modified = true;
                    otherFee = value;
                }
            }
        }

        public decimal StampTaxRatio
        {
            get { return stampTaxRatio; }
            set
            {
                if (stampTaxRatio != value)
                {
                    modified = true;
                    stampTaxRatio = value;
                }
            }
        }

        public decimal BuyTradeRatio
        {
            get { return buyTradeRatio; }
            set
            {
                if (buyTradeRatio != value)
                {
                    modified = true;
                    buyTradeRatio = value;
                }
            }
        }

        public decimal SellTradeRatio
        {
            get { return sellTradeRatio; }
            set
            {
                if (sellTradeRatio != value)
                {
                    modified = true;
                    sellTradeRatio = value;
                }
            }
        }

        #region  functions

        public bool Save()
        {
            XmlSerializer ser = new XmlSerializer(typeof(TradeConfig));

            FileInfo fi = new FileInfo(tradeCfgPath);
            if (fi.Exists)
            {
                fi.IsReadOnly = false;
            }

            FileStream fa = fi.OpenWrite();
            fa.SetLength(0);
            XmlTextWriter wtr = new XmlTextWriter(fa, null);

            wtr.Formatting = Formatting.Indented;
            wtr.Indentation = 4;
            wtr.IndentChar = (char)32;

            ser.Serialize(wtr, this);
            wtr.Close();

            this.Modified = false;
            return true;

        }

        public static TradeConfig Load()
        {
            TradeConfig t;
            try
            {
                XmlSerializer r = new XmlSerializer(typeof(TradeConfig));
                XmlTextReader xr = new XmlTextReader(tradeCfgPath);
                t = (TradeConfig)r.Deserialize(xr);
                xr.Close();
            }
            catch (Exception e)
            {
                t = new TradeConfig();
            }

            t.Modified = false;
            return t;
        }

        #endregion // functions
    }
}
